<template>
    <div>
        <el-card>
            <el-row :gutter="30">
                <el-col :span="10">
                    <el-input placeholder="请输入角菜单名称" v-model="query.permissionName" clearable @clear="getMenuList">
                        <el-button slot="append" icon="el-icon-search" @click="getMenuList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible = true">添加菜单</el-button>
                </el-col>
            </el-row>
            <br />
            <el-table :data="menuList" border stripe>
                <el-table-column type="id" v-if="false">
                </el-table-column>
                <el-table-column prop="permissionName" label="菜单名称" align="center">
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center">
                </el-table-column>
                <el-table-column prop="updateTime" label="更新时间" align="center">
                </el-table-column>
                <el-table-column prop="status" label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tooltip class="itemStatus" effect="dark" :content="scope.row.status == 0 ? '点击启用' : '点击停用' "
                            placement="right">
                            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                                @change="updateStatus(scope.row)"></el-switch>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog title="添加菜单" :visible.sync="addDialogVisible" @open="resetAddMenuForm" width="30%">
            <el-form :model="addMenuForm" ref="menuFormRef" label-width="20%">
                <el-form-item label="菜单名称" prop="permissionName">
                    <el-input v-model="addMenuForm.permissionName"></el-input>
                </el-form-item>
                <el-form-item label="菜单路由" prop="permissionRouter">
                    <el-input v-model="addMenuForm.permissionRouter"></el-input>
                </el-form-item>
                <el-form-item label="菜单icon" prop="icon">
                    <el-input v-model="addMenuForm.icon"></el-input>
                </el-form-item>
                <el-form-item label="菜单顺序" prop="sort">
                    <el-input v-model="addMenuForm.sort"></el-input>
                </el-form-item>
                <el-form-item label="父菜单" prop="parentId">
                    <el-select aria-required="true" @change="changeHandle" v-model="value" clearable
                        placeholder="请选择父菜单">
                        <el-option v-for="item in menuList" :key="item.id" :label="item.permissionName"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addMenu">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        created() {
            this.getMenuList();
        },
        data() {
            return {
                query: {
                    permissionName: null,
                },
                value: null,
                menuList: [],
                addDialogVisible: false,
                editFlag: false,
                addMenuForm: {
                    id: null,
                    permissionName: null,
                    icon: "el-icon-menu",
                    sort: 1,
                    permissionRouter: '/',
                    parentId: -1,
                },
            }
        },
        methods: {
            async getMenuList() {
                if (this.query.permissionName === '') {
                    this.query.permissionName = null;
                }
                const { data: res } = await this.$http.get("/sys/menu/list", { params: this.query });
                this.menuList = res.data;
            },
            async addMenu() {
                if (this.value) {
                    this.addMenuForm.parentId = this.value;
                }
                const { data: res } = await this.$http.post("/sys/menu/add", this.addMenuForm);
                this.addDialogVisible = false;
                this.resetAddMenuForm();
                if (res.data) {
                    this.$message.success((this.addMenuForm.id == null ? "添加" : "编辑") + "菜单成功, 重新登录后生效!");
                    this.getMenuList();
                }
            },
            changeHandle() {
                console.log(this.value);
                if (this.value) {
                    this.parentId = this.value;
                }
            },
            async updateStatus(row) {
                const { data: res } = await this.$http.post("/sys/menu/update", row);
                if (!res.data) {
                    this.getMenuList();
                }
            },
            handleEdit(row) {

            },
            handleDelete(id) {
                this.$elementMessageBox.confirm('删除后将无法恢复, 确认删除吗? ! ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(async () => {
                        const { data: res } = await this.$http.delete("/sys/menu/delete/" + id);
                        if (res.data) {
                            this.$message.success("删除成功!");
                            this.getMenuList();
                            return;
                        }
                        this.$message.error("删除失败!");
                    }).catch(() => {
                        this.$message.info("取消删除!");
                    });
            },
            resetAddMenuForm() {
                try {
                    this.$refs.menuFormRef.resetFields();
                } catch (error) {

                }
            },
        },
    }
</script>